/*
 * Created on 2005-10-11
 *
 */
package com.hd123.rumba.catalog;

import java.util.List;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.domain.IDomain;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.exception.DuplicateIdException;
import com.hd123.rumba.exception.ParameterException;
import com.hd123.rumba.runtime.IManager;
import com.hd123.rumba.runtime.OperateContext;
import com.hd123.rumba.sql.ResultPage;
import com.hd123.rumba.user.IUser;
import com.hd123.rumba.util.LevelCode;

/**
 * @author caili
 */
public interface ICatalogMgr extends IManager {

  public LevelCode getLevelCode(ICatalog catalog) throws BusinessException, HibernateException;

  /**
   * 创建容纳itemType对象的目录. 如果是第一个这种对象类型的目录, 则自动设置成主目录.
   * 
   * @param domain
   * @param id
   * @param itemType
   * @return
   * @throws HibernateException
   */
  public ICatalog createCatalog(IDomain domain, String code, String itemType)
      throws BusinessException, HibernateException;

  public ICatalog getCatalog(String uuid) throws BusinessException, HibernateException;

  public ICatalog getCatalogByCode(IDomain domain, String code) throws BusinessException,
      HibernateException;

  /**
   * 取得容纳ItemType对象的主目录. 如果不存在, 返回空.
   * 
   * @param domain
   * @param itemType
   * @return
   * @throws ParameterException
   * @throws HibernateException
   */
  public ICatalog getMasterCatalog(IDomain domain, String itemType) throws BusinessException,
      HibernateException;

  /**
   * 取得域domain中容纳itemType对象的目录. 如果不存在, 返回的List.size()==0
   * 
   * @param domain
   * @param itemType
   * @return
   * @throws ParameterException
   * @throws HibernateException
   */
  public List createCatalogsList(IDomain domain, String itemType) throws BusinessException,
      HibernateException;

  /**
   * 取得域domain中所有的目录
   * 
   * @param domain
   * @param pageSize
   * @param page
   * @return
   * @throws Exception
   * @throws ParameterException
   */
  public ResultPage createAllCatalogsPage(IDomain domain, int pageSize, int page)
      throws HibernateException, BusinessException;

  /**
   * 保存目录. 如果这个目录被设置为主目录, 则容纳相同对象的其它目录被设置为不是主目录.
   * 
   * @param catalog
   * @throws ParameterException
   * @throws DuplicateIdException
   * @throws HibernateException
   * @deprecated
   */
  public void saveCatalog(ICatalog catalog) throws BusinessException, HibernateException;

  /**
   * 保存目录. 如果这个目录被设置为主目录, 则容纳相同对象的其它目录被设置为不是主目录.
   * 
   * @param catalog
   * @param operator
   * @param oca
   * @throws BusinessException
   * @throws HibernateException
   * @deprecated
   */
  public void saveCatalog(ICatalog catalog, IUser operator, long oca) throws BusinessException,
      HibernateException;

  /**
   * 保存目录. 如果这个目录被设置为主目录, 则容纳相同对象的其它目录被设置为不是主目录.
   * 
   * @param catalog
   * @param operate
   * @throws BusinessException
   * @throws HibernateException
   */
  public void saveCatalog(ICatalog catalog, OperateContext operate) throws BusinessException,
      HibernateException;

  /**
   * 删除该目录. 如果目录中有类别, 则不能删除. 如果这个目录是主目录, 同时不是唯一一个容纳此目录对象的目录, 也不能删除.
   * 
   * @param catalog
   * @deprecated
   */
  public void removeCatalog(ICatalog catalog, IUser operator) throws BusinessException, HibernateException;

  /**
   * 删除该目录. 如果目录中有类别, 则不能删除. 如果这个目录是主目录, 同时不是唯一一个容纳此目录对象的目录, 也不能删除.
   * 
   * @param catalog
   */
  public void removeCatalog(ICatalog catalog, OperateContext operate) throws BusinessException, HibernateException;

  // /////////////////////////////////////
  //
  // Category
  //

  /**
   * 创建目录catalog中的类别. id需要满足此目录的类别级别要求. 如果id所表示的类别不是顶级类别, 则其父类别必须存在.
   * 
   * @param catalog
   * @param id
   * @return
   */
  public ICategory createCategory(ICatalog catalog, String code) throws BusinessException,
      HibernateException;

  public boolean isValidCategoryId(ICatalog catalog, String code);

  /**
   * 分析给定的子类别的code, 取得父类别
   * 
   * @param catalog
   * @param code
   *          子类别的code
   * @return 如果id表示的类别是顶级类别, 则返回null
   * @throws BusinessException:
   *           code表示的父类别不存在; code的长度非法
   * @throws ParameterException
   * @throws HibernateException
   */
  public ICategory getParentCategoryByCode(ICatalog catalog, String code) throws BusinessException,
      HibernateException;

  public ICategory getCategory(String uuid) throws HibernateException, BusinessException;

  public ICategory getCategoryByCode(ICatalog catalog, String code) throws BusinessException,
      HibernateException;

  /**
   * 保存category. 检查id是否合法, 是否在目录范围内重复. 并根据id设置parent
   * 
   * @param category
   * @throws ParameterException
   * @throws HibernateException
   * @throws BusinessException
   * @deprecated
   */
  public void saveCategory(ICategory category) throws HibernateException, BusinessException;

  /**
   * 保存category. 检查id是否合法, 是否在目录范围内重复. 并根据id设置parent
   * 
   * @param category
   * @param operator
   * @param oca
   * @throws HibernateException
   * @throws BusinessException
   * @deprecated
   */
  public void saveCategory(ICategory category, IUser operator, long oca) throws HibernateException,
      BusinessException;

  /**
   * 保存category. 检查id是否合法, 是否在目录范围内重复. 并根据id设置parent
   * 
   * @param category
   * @param operate
   * @throws HibernateException
   * @throws BusinessException
   */
  public void saveCategory(ICategory category, OperateContext operate) throws HibernateException,
      BusinessException;

  /**
   * 删除类别. 如果此类别有子类别, 不能删除. 不判断是否有其它对象使用了此类别.
   * 
   * @param category
   * @throws HibernateException
   * @throws BusinessException
   * @deprecated
   */
  public void removeCategory(ICategory category, IUser operator) throws HibernateException, BusinessException;

  /**
   * 删除类别. 如果此类别有子类别, 不能删除. 不判断是否有其它对象使用了此类别.
   * 
   * @param category
   * @throws HibernateException
   * @throws BusinessException
   */
  public void removeCategory(ICategory category, OperateContext operate) throws HibernateException, BusinessException;

  /**
   * 取得该目录中的类别, 按类别Id的升序排列. <br>
   * 如果该目录没有类别, 返回的List.size() == 0.
   * 
   * @param mode:
   *          ALLCHILDREN=所有类别, DIRECTCHILDREN=顶级类别
   * @param pageSize:
   *          每页包含的记录数. 0=全部记录.
   * @param page:
   *          第几页.
   */
  public ResultPage createCatalogCategoriesPage(ICatalog catalog, int mode, int pageSize, int page)
      throws BusinessException, HibernateException;

  /**
   * 取得子类别. 按子类别的Id升序排列. 如果没有子类别, 返回的List.size()==0.
   * 
   * @param mode:
   *          ALLCHILDREN = 所有子类别, DIRECTCHILDREN=直接的子类别
   * @param pageSize:
   *          每页包含的记录数. 0=全部记录.
   * @param page:
   *          第几页.
   * @return
   */
  public ResultPage createChildCategoriesPage(ICategory category, int mode, int pageSize, int page)
      throws BusinessException, HibernateException;

  public boolean hasChildren(ICategory category) throws BusinessException, HibernateException;

  /**
   * 是否是最后一级的类别。根据catalog的level长度进行判断。
   * 
   * @param category:
   *          需要检查的类别
   * @return
   */
  public boolean isLastChildren(ICategory category) throws BusinessException, HibernateException;

  public static final int ALLCHILDREN = 0;
  public static final int DIRECTCHILDREN = 1;

}
